﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using cludg.Web.Core.Validation;
using cludg.Web.Core.ViewModels;

namespace cludg.Web.ViewModels
{
	[CustomValidation(typeof(RegisterModelValidator), "ValidateRegisterModel")]
	public class RegisterModel : BaseModel
	{
		[Required(ErrorMessage = ValidationConst.RequiredMessage)]
		[StringLength(100, ErrorMessage = ValidationConst.StringLengthMaxInvalidMessage)]
		[DisplayName("Имя пользователя")]
		public string Name { get; set; }

		[Required(ErrorMessage = ValidationConst.RequiredMessage)]
		[StringLength(100, ErrorMessage = ValidationConst.StringLengthMaxInvalidMessage)]
		[DisplayName("Email")]
		[RegularExpression(ValidationConst.EmailRegEx, ErrorMessage = ValidationConst.EmailIsIvalidErrorMessage)]
		public string Email { get; set; }

		[Required(ErrorMessage = ValidationConst.RequiredMessage)]
		[StringLength(20, MinimumLength = 6, ErrorMessage = ValidationConst.StringLengthMaxMinInvalidMessage)]
		[DisplayName("Пароль")]
		public string Password { get; set; }

		[Required(ErrorMessage = ValidationConst.RequiredMessage)]
		[StringLength(20, MinimumLength = 6, ErrorMessage = ValidationConst.StringLengthMaxMinInvalidMessage)]
		[DisplayName("И еще раз пароль")]
		public string PasswordConfirm { get; set; }
	}

	public class RegisterModelValidator
	{
		public static ValidationResult ValidateRegisterModel(RegisterModel model)
		{
			if (model.Password != model.PasswordConfirm)
				return new ValidationResult(ValidationConst.PasswordAndConfirmNotIdenticalMessage,
					new List<string> { "Password", "PasswordConfirm" });

			return ValidationResult.Success;
		}
	}
}